<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Mydemand extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'mydemand';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'type_text',
        'starttime_text',
        'endtime_text',
        'apporval_text',
        'appr_time_text',
        'one_admin_id_text'
    ];
    

    
    public function getTypeList()
    {
        return ['sickleave' => __('Type sickleave'), 'compassionateleave' => __('Type compassionateleave'), 'businesstravel' => __('Type businesstravel')];
    }

    public function getApporvalList()
    {
        return ['0' => __('Apporval 0'), '1' => __('Apporval 1'), '2' => __('Apporval 2')];
    }
    public function admin()
    {
        return $this->hasOne('admin','id','user_id');
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getApporvalTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['apporval']) ? $data['apporval'] : '');
        $list = $this->getApporvalList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getApprTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['appr_time']) ? $data['appr_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setApprTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

     //修改用户id
   protected function setOneAdminIdAttr($value)
   {
       //有可能是excel提交的，有可能是添加的 都是字符串
       //只有数字和逗号
       $par = '/^[(0-9)(\,)+]+$/';
       if(!preg_match($par, $value)){
           $value = explode(',',$value);
           foreach($value as $v){
               $arr[] = Admin::where('nickname',$v)->value('id');
           }
           return implode(',',$arr);
       }
       return $value;
   }

   public function getOneAdminIdTextAttr($value,$row){

       $arr_value = explode(',',$row['one_admin_id']);
       foreach($arr_value as $v){
           $arr[] = Admin::where('id',$v)->value('nickname');
       }
       return implode(',',$arr);
   }



}
